//
//  PostHelper.m
//  jandaobao
//
//  Created by 杨志刚 on 15/5/27.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "PostHelper.h"
#import "ParamsHelper.h"
#import <AssetsLibrary/ALAsset.h>

#import <AssetsLibrary/ALAssetsLibrary.h>

#import <AssetsLibrary/ALAssetsGroup.h>

#import <AssetsLibrary/ALAssetRepresentation.h>

@implementation PostHelper

+(void)post:(NSString *)url :(NSMutableDictionary *)params :(void (^)(id))success{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
   // [manager.requestSerializer setTimeoutInterval:45];
    [SVProgressHUD showWithStatus:@"请稍等"];
    DLog(@"申请URL：%@",url);
    DLog(@"申请参数：%@",params);
    
    [manager POST: url
       parameters: params
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
              
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        success(dic);
              
        [SVProgressHUD dismiss];
      
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:KCONNECTFAILURE];
         success(NULL);
        }];
}




+(void)postAnswer:(NSString *)url
                 :(NSMutableDictionary *)params
                 :(void (^)(id))success{
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
   // [SVProgressHUD showWithStatus:@"正在交卷.."];
    [manager.requestSerializer setTimeoutInterval:200];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    

    DLog(@"申请URL：%@",url);
    DLog(@"申请参数：%@",params);
    
    //这块使用GET请求方法 因为字节超多会出现 request too long 的错误。
    //使用post请求,会提交不上试题
    [manager POST: url parameters:params
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
////        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//        MMHudWLog(@"%@",string);
        success(responseObject);
       // [SVProgressHUD dismiss];
     
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        //[MMProgressHUD dismissWithError:KCONNECTFAILURE ];
       //// [SVProgressHUD showErrorWithStatus:KCONNECTFAILURE];
        //[SVProgressHUD dismiss];
        success(error.userInfo);
 
    }];
}


+(void)postNoHUD:(NSString *)url
                :(NSMutableDictionary *)params
                :(void (^)(id))success{
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    DLog(@"申请URL：%@",url);
    DLog(@"申请参数：%@",params);
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    [manager.requestSerializer setTimeoutInterval:45];
    [manager POST: url
       parameters:params
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        success(NULL);
    }];
}


+(void)postQuestion:(NSString *)url
                   : (NSMutableDictionary *) params
                   : (NSData *)audioData
                   : (void (^)(id t)) success{
    
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setTimeoutInterval:45];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    
    [SVProgressHUD showWithStatus:@"请稍等"];
  //  manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:url
               parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        DLog(@"申请的url%@",url);
        DLog(@"申请的参数%@",params);
        DLog(@"申请的autioData%@",audioData);
        
    [formData appendPartWithFileData:audioData
                                name:@"attachment_voice"
                            fileName:[NSString stringWithFormat:@"autio.amr"]
                            mimeType:@"audio/amr/wav"];

        
    } success:^(AFHTTPRequestOperation *operation,id responseObject) {
        success(responseObject);
        [SVProgressHUD dismiss];

    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
        success(error.userInfo);
        [SVProgressHUD showErrorWithStatus:KCONNECTFAILURE];
       // [SVProgressHUD dismiss];
    }];

}


+(void)postImg:(NSString *)url
              :(NSMutableDictionary *)params
              :(NSData *)imgData
              :(void (^)(id t)) success{

    
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setTimeoutInterval:45];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    
    [SVProgressHUD showWithStatus:@"正在上传"];


    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:url
       parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        DLog(@"申请的url%@",url);
        DLog(@"申请的参数%@",params);
                
    [formData appendPartWithFileData:imgData
                                name:@"images_files"
                            fileName:[NSString stringWithFormat:@"updateImg.png"]
                            mimeType:@"image/png"];
        
        
    } success:^(AFHTTPRequestOperation *operation,id responseObject) {
      
        success((NSDictionary *)responseObject);
       
       // [SVProgressHUD dismiss];

        
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
        success(error.userInfo);
            
        DLog(@"%@",error.userInfo);

        [SVProgressHUD showErrorWithStatus:KCONNECTFAILURE];
       // [SVProgressHUD dismiss];

        
    }];
    



}

//公用的上传图片
+(void)postImg:(NSString *)url
              :(NSMutableDictionary *)params
              :(NSData *)imgData
              :(NSString *)dataParamName
              :(void (^)(id t)) success{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager POST:url
       parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        DLog(@"申请的url%@",url);
        DLog(@"申请的参数%@",params);
        
        [formData appendPartWithFileData:imgData
                                    name:dataParamName
                                fileName:[NSString stringWithFormat:@"updateImg.png"]
                                mimeType:@"image/png"];
        
        
    } success:^(AFHTTPRequestOperation *operation,id responseObject) {
 
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        success(dic);
        
 
        
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
        success(error.userInfo);
        
        DLog(@"%@",error.userInfo);
    }];
    
}



+ (void)postAddActiveData:(NSString *)url
                         :(NSMutableDictionary *)params
                         :(NSData *)imageData
                         :(void (^)(id))success
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setTimeoutInterval:45];
    
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    
//    [MMProgressHUD showWithTitle:@"请稍等" status:@"上传中.." cancelBlock:^{
//        [[manager operationQueue] cancelAllOperations];
//        [MMProgressHUD dismiss];
//    }];
    
    DLog(@"申请的URL%@",url);
    DLog(@"申请的参数%@",params);
   // MMHudWLog(@"申请的data%@",imageData);
    [SVProgressHUD showWithStatus:@"上传中..."];
    
    [manager POST:url
       parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        
        // NSData *data = UIImageJPEGRepresentation(imageTo, 0.7);
        if(imageData !=nil){
            
            [formData appendPartWithFileData:imageData
                                        name:[NSString stringWithFormat:@"fileUpload"]
                                    fileName:[NSString
                              stringWithFormat:@"fileUpload.png"]
                                    mimeType:@"image/png"];
        }

        
    } success:^(AFHTTPRequestOperation *operation,id responseObject) {
        
        
        
        success(responseObject);
        [SVProgressHUD dismiss];
        //[MMProgressHUD dismiss];
        
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
        if (![operation isCancelled]) {
            
//            DLog(@"%@",error.userInfo);
//            [MMProgressHUD dismissWithError:KCONNECTFAILURE afterDelay:1.5];
            
            [SVProgressHUD dismiss];
        }
    }];


}

+(void)postAddActive:(NSString *)url
                    : (NSMutableDictionary *) params
                    : (NSString *) fileAddress
                    : (void (^)(id t)) success{

    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setTimeoutInterval:45];
    
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    
    DLog(@"申请URL：%@",url);
    DLog(@"申请参数：%@",params);
//    [MMProgressHUD dismiss];
    [manager POST:url
       parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
       // NSData* pData = [NSData dataWithContentsOfFile: mp3FilePath];
        NSData *data = [NSData dataWithContentsOfFile:fileAddress];
     
//        MMHudWLog(@"--------%@",fileAddress);
        if ([[params objectForKey:@"msgType"] intValue]==3) {
            
            [formData appendPartWithFileData:data
                                        name:[NSString stringWithFormat:@"fileUpload"]
                                    fileName:[NSString stringWithFormat:@"fileUpload.amr"] mimeType:@"audio/amr/wav"];
        }else if ([[params objectForKey:@"msgType"] intValue]==2)
        {
        
          [formData appendPartWithFileData:data
                                      name:[NSString stringWithFormat:@"fileUpload"]
                                  fileName:[NSString stringWithFormat:@"fileUpload.png"] mimeType:@"image/png"];
        }
        
        //[formData appendPartWithFileURL:[NSURL URLWithString:fileAddress] name:@"fileUpload" error:nil];
       // [formData appendPartWithFileURL:[NSURL URLWithString:fileAddress] name:@"UploadeFile.png" fileName:@"fileUpload" mimeType:@"image/png" error:NULL];
        
        //[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file"] fileName:[NSString stringWithFormat:@"UploadeFile.png"] mimeType:@"image/png"];
      //  }
        
    } success:^(AFHTTPRequestOperation *operation,id responseObject) {
        
        success(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
        if (![operation isCancelled]) {
           // [OMGToast  showWithText:@"发送失败"];
        }
    }];
    

}


+(void)get:(NSString *)url :(NSMutableDictionary *)params :(void (^)(id))success{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
  
//    [MMProgressHUD showWithTitle:@"请稍等" status:@"" cancelBlock:^{
//        [[manager operationQueue] cancelAllOperations];
//        [MMProgressHUD dismiss];
//    }];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    
    [SVProgressHUD showWithStatus:@"请稍等"];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    
    DLog(@"申请URL：%@",url);
    DLog(@"申请参数：%@",params);
    [manager GET:url
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
      
      
        success(responseObject);
//        [MMProgressHUD dismiss];
        [SVProgressHUD dismiss];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//        [MMProgressHUD dismissWithError:KCONNECTFAILURE];
    
        success(NULL);
        [SVProgressHUD showErrorWithStatus:KCONNECTFAILURE];
        //[SVProgressHUD dismiss];
//         MMHudWLog(@"返回错误结果%@",error.userInfo);
    }];
}




@end

